<template>
	<view class="loginPage">
		<uni-notice-bar v-show="showBar" scrollable style="position: absolute;top: 0;width: 100%;" show-icon single
			text="当前回执需进行实名认证,请前往认证信息进行认证" />
		<view class="topBox">
			<image class="avatar" :src="userInfo.avatarUrl?userInfo.avatarUrl:userAUrl"></image>
			<view class="formLable">
				<view style="display: flex;">
					<view v-if="userInfo.phone" class="lableItem">
						{{namePass(userInfo.name)}}
					</view>
					<view v-else class="lableItem" @click="loginCheckChange">
						请登录系统
					</view>
					<view class="lableItem" style="flex: 1;">
						{{phonePass(userInfo.phone)||''}}
					</view>
				</view>
				<view class="lableItem">
					{{idCardPass(userInfo.idCard)}}
				</view>
			</view>
			<view @click="updateInfo" style="position: absolute;right: 10px;bottom: 10px;color: #fff;">
				认证信息
			</view>
			<view class="pass">
				<image class="image" :src="'/static/ver'+img+'.png'" mode="">
				</image>
				<text>认证</text>
				<text>(签名{{userInfo.sign?'已':'未'}}设置)</text>
			</view>
		</view>
		<view style="height: 20px;">

		</view>
		<!-- <uni-list v-if="status==0">
			<uni-list-item thumb="/static/share.png" thumb-size="sm" link @click="toShare" title="签收提醒" />
		</uni-list>
		<uni-list v-if="status==1">
			<uni-list-item thumb="/static/scan.png" thumb-size="sm" link @click="scanCode" title="扫码签收" />
			<uni-list-item thumb="/static/toWebView.png" thumb-size="sm" link @click="toWebView" title="签收记录" />
		</uni-list> -->
		<uni-list>
			<uni-list-item v-if="status>0" thumb="/static/scan.png" thumb-size="sm" link @click="scanCode"
				title="扫码签收" />
			<uni-list-item v-if="status>0" thumb="/static/toWebView.png" thumb-size="sm" link @click="toWebView"
				title="签收记录" />
			<uni-list-item v-if="status!=1" thumb="/static/share.png" thumb-size="sm" link @click="toShare" title="回执跟踪"
				:show-badge="badgeNum>0" :badge-text="badgeNum" :badgeStyle="{
					background:'#d50700'
				}" />
		</uni-list>
		<uni-popup ref="mask" type="top" backgroundColor="rgba(0,0,0,0.2)" height="100vh"></uni-popup>

	</view>
</template>

<script>
	import {
		idCardPass,
		namePass,
		phonePass
	} from '@/utils/PrivacyPass.js'

	export default {

		data() {
			return {
				code: "",
				disabled: false,
				show: false,
				userAUrl: "https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132",
				userInfo: {},
				type: '0',
				lType: '0',
				showBar: false,
				flag: true,
				img: '3',
				status: "",
				badgeNum: 0,
				query: null
			};
		},
		async onShow() {
			// 手机型号
			uni.getSystemInfo({
				success: (res) => {
					console.log(res);
					uni.setStorageSync('DeviceName', res.model)
					uni.setStorageSync('DeviceId', res.deviceId)
				}
			})
			let obj = uni.getStorageSync('userInfo')
			this.userInfo = typeof obj == 'object' ? obj : {}
			this.AuthRealName()
			// APP 缓存获取
			setTimeout(() => {
				let APPQuery = uni.getStorageSync('APPQuery')
				if (APPQuery) {
					// 跳过实名验证
					if (JSON.parse(APPQuery).Type == 3) {

						this.$refs.mask.close()
						uni.hideLoading();
						uni.navigateTo({
							url: '/pages/Reconciliation/Reconciliation?data=' + APPQuery
						})
						uni.removeStorageSync('APPQuery')
					} else this.toReconciliation(APPQuery)
				}

			}, 300)
			// 判断显示是否显示签收提醒等区块
			this.GetWxUserPlace()
			if (this.status != 1) {
				this.$http('POST', '/Base_Manage/Base_User/GetWxCompayList?Phone=' + this.userInfo.phone).then(res => {
					if (res.Success) {
						let str = res.Data.join(',')
						this.$http('post', '/BO/BillList/GetWXBillCount', {
							Status: 0,
							SupplieId: str
						}).then(res => {
							this.badgeNum = res.Data
						})
					}
				})
			}
		},
		async onLoad(query) {
			let obj = uni.getStorageSync('userInfo')
			this.userInfo = typeof obj == 'object' ? obj : {}
			if (!this.userInfo.phone) {
				uni.redirectTo({
					url: '/pages/authorization/authorization?query=' + JSON.stringify(query)
				})
				return
			}
			this.query = query
			await this.AuthRealName()
			await this.GetWxUserPlace()

			console.log(this.status);

			// query.data 移动端APP进入  本地存储
			if (query.data) {
				this.$refs.mask.open()
				uni.showLoading({
					mask: true,
					title: '身份验证中...'
				})
				if (query.data[0] == '{') uni.setStorageSync('APPQuery', query.data)
				else {
					// 手动将字符串拆成对象
					let obj = {}
					query.data.split(',').forEach(item => {
						let keyValue = item.split(':')
						obj[keyValue[0]] = keyValue[1]
					})
					uni.setStorageSync('APPQuery', JSON.stringify(obj))
				}
			}
			// 单纯发货方不执行以下任何操作
			if (this.status === 0) return
			let q = decodeURIComponent(query.q)
			if (q.split(',').length > 1) {
				q = JSON.parse(q).q
				this.$refs.mask.open()
				uni.showLoading({
					mask: true,
					title: '身份验证中...'
				})
			}
			setTimeout(() => {
				let APPQuery = uni.getStorageSync('APPQuery')
				if (q.split('=').length > 2) {
					let arr = q.split('?')
					this.jump(arr)
				}
			}, 10);
		},
		methods: {

			// 判断是否存在手机号 进行系统登录
			loginCheckChange() {
				if (!this.userInfo.phone) {
					uni.redirectTo({
						url: '/pages/authorization/authorization?query=' + JSON.stringify(this.query)
					})
					return
				}
			},
			// 判断显示是否显示签收提醒等区块
			async GetWxUserPlace() {
				await this.$http('POST', '/Base_Manage/Base_User/GetWxUserPlace?Phone=' + this.userInfo.phone).then(
					res => {
						this.status = res.Data
					})
			},
			// 签收提醒
			toShare() {
				uni.navigateTo({
					url: '/pages/Share/Share'
				})
			},
			// APP跳转
			async toReconciliation(APPQuery) {
				await this.AuthRealName()
				if (this.flag) return this.showModel('签收对账单需进行实名认证,请前往认证信息进行认证')
				uni.removeStorageSync('APPQuery')

				this.$refs.mask.close()
				uni.hideLoading();
				uni.navigateTo({
					url: '/pages/Reconciliation/Reconciliation?data=' + APPQuery
				})
			},
			// 隐私加密
			idCardPass(text) {
				return idCardPass(text)
			},
			namePass(text) {
				return namePass(text)
			},
			phonePass(text) {
				return phonePass(text)
			},
			// 认证信息
			updateInfo() {
				// 如果不存在手机号。登录系统
				if (!this.userInfo.phone) {
					uni.showToast({
						title: '请先登录系统',
						duration: 2000,
						icon: 'none'
					})
					return
				}

				this.$refs.mask.close()
				uni.hideLoading();
				uni.navigateTo({
					url: '/pages/UpdateInfo/UpdateInfo'
				})
			},
			// 扫码
			scanCode() {
				// 如果不存在手机号。登录系统
				if (!this.userInfo.phone) {
					uni.showToast({
						title: '请先登录系统',
						duration: 2000,
						icon: 'none'
					})
					return
				}
				uni.scanCode({
					onlyFromCamera: true,
					success: (res) => {
						console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
						let arr = res.result.split('?')
						this.jump(arr)
					}
				});
			},
			// 历史记录
			toWebView() {
				// 如果不存在手机号。登录系统
				if (!this.userInfo.phone) {
					uni.showToast({
						title: '请先登录系统',
						duration: 2000,
						icon: 'none'
					})
					return
				}
				this.$refs.mask.close()
				uni.hideLoading();
				uni.navigateTo({
					url: '/pages/webView/webView?phone=' + this.userInfo.phone
				})
			},
			// 跳转
			async jump(arr) {
				if (arr.length > 1 && (arr[0] == 'http://hzq.yoojet.com' || arr[0] ==
						'https://hzq.yoojet.com')) {
					let newArr = arr[1].split('&')
					// [0] ID 
					// [1] 验证方式
					// [2] 位置验证
					// [3] 位置距离
					// [4] 是否跳转验证
					uni.setStorageSync('Id', newArr[0].split('=')[1]);
					this.type = newArr[1].split('=')[1]
					this.lType = newArr[2].split('=')[1]
					let url = '/pages/Page/preview'
					if (newArr.length >= 5) {
						let jumpType = newArr[4].split('=')[1]
						if (jumpType == '1') url = '/pages/Page/Page'
					}
					if (this.type == '3') {

						this.$refs.mask.close()
						uni.hideLoading();
						uni.navigateTo({
							url
						})
						return
					}
					uni.setStorageSync('type', this.type)
					// 是否存储位置距离
					if (this.lType == '1') {
						uni.setStorageSync('LType', newArr[3].split('=')[1])
					} else {
						uni.removeStorageSync('LType')
					}
					await this.AuthRealName()
					// 姓名身份证不存在
					if (this.flag) return this.showModel('签收回执单需进行实名认证,请前往认证信息进行认证')
					// 授权码
					let bl = await this.authCode()
					if (!bl) return uni.showToast({
						icon: 'none',
						title: '签收授权码错误'
					})
					this.$refs.mask.close()
					uni.hideLoading();
					uni.navigateTo({
						url
					})
				} else {
					uni.showToast({
						title: '请扫描正确的二维码',
						icon: 'none'
					})
				}
			},
			// 授权码
			async authCode() {
				let bl = false
				let id = uni.getStorageSync('Id')
				let smscode = this.userInfo.smscode ? this.userInfo.smscode : ''
				await this.$http('POST', '/BO/BillList/VerificationCode?Id=' + id + '&smsode=' + smscode)
					.then(
						res => {
							if (res.Success && res.Data) {
								bl = true
							} else {
								bl = false
							}
						})
				return bl
			},
			// 提示弹窗
			showModel(title) {
				this.$refs.mask.close()
				uni.hideLoading();
				uni.showModal({
					title: '提示',
					content: title,
					showCancel: false,
					success: (val) => {
						if (val.confirm) {
							uni.navigateTo({
								url: '/pages/UpdateInfo/UpdateInfo'
							})
						}
					}
				})
			},
			// 验证是否实名认证
			async AuthRealName() {
				this.flag = false
				await uni.request({
					url: this.$veriUrl + '/auth/VeriRecord/state?Value=' + this.userInfo.phone,
					method: 'GET'
				}).then(result => {
					let [err, res] = result;
					if (res.data.code == 200) {
						console.log(res.data.data);
						this.flag = false
						this.img = res.data.data.veriType
						this.userInfo = {
							...res.data.data,
							name: res.data.data.name || this.userInfo.name,
							phone: res.data.data.phone || this.userInfo.phone,
							idCard: res.data.data.idNumber || this.userInfo.idCard,
							avatarUrl: res.data.data.avatarUrl || this.userInfo.avatarUrl,
							smscode: res.data.data.smscode || this.userInfo.smscode,
						}
						if (this.userInfo.name) this.getBG(this.userInfo.name)
						else uni.removeStorageSync('nameBG')
					} else {
						this.flag = true
					}
					uni.setStorageSync('userInfo', this.userInfo)
				})
				return this.flag
			},
			getBG(name) {
				this.$http('post', '/Base_Manage/Upload/GetNameSign?Name=' + name).then(res => {
					if (res.Success) uni.setStorageSync('nameBG', res.Data)
				})
			}
		},
	}
</script>

<style lang="scss">
	.uni-list-item__container {
		padding-top: 20px !important;
		padding-bottom: 20px !important;
	}

	.uni-list-item__content-title {
		font-size: 18px !important;
	}

	.uni-list-item {
		background-color: #ff8a8626 !important;
	}

	.uni-list--sm {
		width: 30px !important;
		height: 30px !important;
	}

	.formLable {
		display: flex;
		flex-direction: column;
		padding: 0 40rpx;
		flex: 1;
		color: #fff;

		.lableItem {
			height: 45px;
			line-height: 45px;
			margin-right: 10px;
		}
	}

	.loginPage {
		background-color: #f7f7f7;
		height: 100vh;
	}

	.topBox {
		overflow: hidden;
		box-sizing: border-box;
		display: flex;
		padding: 40px 20px;
		position: relative;
		background-color: #d50700;
		color: #fff;

		.pass {
			position: absolute;
			height: 30px;
			// width: 90px;
			bottom: 5px;
			display: flex;
			align-items: center;
			justify-content: center;

			.image {
				width: 32px;
				height: 32px;
				margin-right: 10px;
			}
		}
	}

	.avatar {
		width: 90px;
		height: 90px;
		border-radius: 5px;
	}
</style>